//
//  XHTabBar.swift
//  gu-新浪微博
//
//  Created by xiaohui on 16/6/25.
//  Copyright © 2016年 gusheghui. All rights reserved.
//

import UIKit
//声明协议
protocol XHTabBarDelegate:NSObjectProtocol{
    func composeButtonClick()
}
class XHTabBar: UITabBar {
// 声明代理
    weak var xhDelegate:XHTabBarDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUi()
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        
        let weight = SCREENW / 5
        var index: CGFloat = 0
        
        for value in self.subviews {
            if value .isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                value.frame.size.width = weight
                value.frame.origin.x = index * weight
                index++
                
                if index == 2{
                    index++
                }
                
            }
         
        }
        
        
        
        composeButton.center.x = self.frame.width/2
        composeButton.center.y = self.frame.height/2
        
    }
    
    func setupUi(){
        addSubview(composeButton)
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //监听方法
    @objc private func buttonClick(){
        xhDelegate?.composeButtonClick()

    }
    
    
    //懒加载中间button
    private lazy var composeButton: UIButton = {
        let button = UIButton()

        button.addTarget(self, action:"buttonClick", forControlEvents: UIControlEvents.TouchUpInside)

          button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)

       button.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Selected)

        
        button.sizeToFit()
        
        return button
    }()
    

}
